/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import com.toedter.calendar.*;
import com.beans.Projets;
import com.beans.Prospects;

import com.metier.ProjetMetier;
import com.metier.ProspectsMetier;

/**
 * 
 * @author Mamadou
 */

/**
 * 
 * @author Mamadou
 */
public class PanelProspects extends javax.swing.JPanel implements MouseListener {

	private javax.swing.JButton boutonAjouter;
	private javax.swing.JButton boutonImprimer;
	private javax.swing.JButton boutonModifier;
	private javax.swing.JButton boutonRechercher;
	private javax.swing.JButton boutonSupprimer;
	private javax.swing.JLabel jLabel1, textLabel;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JPanel panelBoutons, panelBouton1;
	private javax.swing.JPanel panelbas;
	private javax.swing.JPanel panelhaut;
	private JTextField textRechercher;
	private JLabel labelDateDebut, labelDateFin;
	private com.toedter.calendar.JDateChooser dateDebutProspect;
	private com.toedter.calendar.JDateChooser dateFinProspect;
	DefaultTableModel dtm;
	ProspectsMetier prospectMetier;
	Prospects prospect;
	private JButton boutonAnnuler;
	private JTable tableListeProspects;
	private JButton buttonAnnuler;

	// End of variables declaration//GEN-END:variables

	public PanelProspects() {
		prospectMetier = new ProspectsMetier();
		prospect = new Prospects();
		initComponents();
		
		getTableListeProspects().addMouseListener(this);
		boutonSupprimer.setEnabled(false);
		boutonModifier.setEnabled(false);

	}

	private void initComponents() {

		panelhaut = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		panelbas = new javax.swing.JPanel();
		panelbas.setLayout(new GridLayout(1, 1));
		panelBoutons = new javax.swing.JPanel();
		panelBouton1 = new JPanel();
		labelDateDebut = new JLabel("DATE DEBUT");
		labelDateFin = new JLabel("DATE FIN");
		buttonAnnuler = new JButton("ANNULER");
		buttonAnnuler.setFont(new java.awt.Font("Verdana", 3, 14));
		boutonAjouter = new javax.swing.JButton();
		boutonSupprimer = new javax.swing.JButton();
		boutonModifier = new javax.swing.JButton();
		boutonImprimer = new javax.swing.JButton();
		boutonImprimer.setVisible(false);
		boutonRechercher = new javax.swing.JButton();
		boutonAnnuler = new JButton("ANNULER");
		boutonAnnuler.setFont(new java.awt.Font("Verdana", 3, 14));
		jLabel2 = new javax.swing.JLabel();
		textLabel = new JLabel("");
		textRechercher = new JTextField();
		dateDebutProspect = new com.toedter.calendar.JDateChooser();
		dateFinProspect = new com.toedter.calendar.JDateChooser();
		labelDateDebut.setFont(new java.awt.Font("Verdana", 3, 12));
		labelDateDebut.setForeground(new java.awt.Color(255, 255, 255));
		labelDateFin.setForeground(new java.awt.Color(255, 255, 255));
		labelDateFin.setFont(new java.awt.Font("Verdana", 3, 12));

		setBackground(new java.awt.Color(128, 130, 133));
		setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1,
				new java.awt.Color(102, 153, 255)));
		setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		setMaximumSize(new java.awt.Dimension(1050, 550));
		setMinimumSize(new java.awt.Dimension(1050, 550));
		setLayout(new java.awt.BorderLayout());
		panelBoutons.setLayout(new BorderLayout());
		panelBouton1.setBackground(new java.awt.Color(128, 130, 133));
		panelBoutons.setBackground(new java.awt.Color(128, 130, 133));

		panelhaut.setBackground(new java.awt.Color(128, 130, 133));
		panelhaut.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1,
				1, 1, new java.awt.Color(103, 153, 255)));
		panelhaut.setPreferredSize(new java.awt.Dimension(1050, 75));

		jLabel1.setIcon(new javax.swing.ImageIcon("src/main/java/com/photos/gestion des prospects.jpg")); // NOI18N

		panelbas.setBackground(new java.awt.Color(128, 130, 133));
		panelbas.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1,
				1, new java.awt.Color(102, 153, 255)));
		panelbas.setMaximumSize(new java.awt.Dimension(1050, 375));
		panelbas.setMinimumSize(new java.awt.Dimension(1050, 375));
		panelbas.setPreferredSize(new java.awt.Dimension(1056, 410));
		panelBoutons.setBackground(new java.awt.Color(128, 130, 133));
		panelBoutons.setPreferredSize(new java.awt.Dimension(742, 35));
		panelBoutons.setBorder(javax.swing.BorderFactory.createMatteBorder(1,
				1, 1, 1, new java.awt.Color(103, 153, 255)));
		panelBoutons.setMaximumSize(new java.awt.Dimension(1050, 50));
		panelBoutons.setMinimumSize(new java.awt.Dimension(1050, 50));
		panelBoutons.setPreferredSize(new java.awt.Dimension(1050, 40));

		boutonAjouter.setIcon(new javax.swing.ImageIcon("src/main/java/com/photos/add16.png")); // NOI18N
		boutonAjouter.setToolTipText("Ajouter");
		boutonAjouter.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				jLabel2.setVisible(false);
				boutonImprimer.setVisible(false);
				boutonModifier.setVisible(false);
				boutonRechercher.setVisible(false);
				boutonSupprimer.setVisible(false);
				textRechercher.setVisible(false);
				boutonAjouter.setVisible(false);
				labelDateDebut.setVisible(false);
				labelDateFin.setVisible(false);
				dateFinProspect.setVisible(false);
				dateDebutProspect.setVisible(false);
				panelbas.removeAll();
				panelbas.removeAll();
				PanelAjouterProspect panAjoutProspect = new PanelAjouterProspect(
						boutonAnnuler);
				panelbas.add(panAjoutProspect, BorderLayout.SOUTH);

				validate();

			}
		});
		boutonAnnuler.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				boutonImprimer.setVisible(false);
				boutonModifier.setVisible(true);
				boutonRechercher.setVisible(true);
				boutonSupprimer.setVisible(true);
				textRechercher.setVisible(true);
				boutonAjouter.setVisible(true);
				labelDateDebut.setVisible(true);
				labelDateFin.setVisible(true);
				dateFinProspect.setVisible(true);
				dateDebutProspect.setVisible(true);
				panelbas.removeAll();
				panelbas.add(new JScrollPane(tableListeProspects),
						BorderLayout.CENTER);
				validate();

			}
		});
		buttonAnnuler.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				boutonImprimer.setVisible(false);
				boutonModifier.setVisible(true);
				boutonRechercher.setVisible(true);
				boutonSupprimer.setVisible(true);
				textRechercher.setVisible(true);
				boutonAjouter.setVisible(true);
				labelDateDebut.setVisible(true);
				labelDateFin.setVisible(true);
				dateFinProspect.setVisible(true);
				dateDebutProspect.setVisible(true);
				panelbas.removeAll();
				panelbas.add(new JScrollPane(tableListeProspects),
						BorderLayout.CENTER);
				validate();

			}
		});
		boutonSupprimer.setIcon(new javax.swing.ImageIcon("src/main/java/com/photos/supprimer.gif")); // NOI18N
		boutonSupprimer.setToolTipText("Supprimer");

		boutonModifier.setIcon(new javax.swing.ImageIcon("src/main/java/com/photos/modifier.gif"));
		boutonModifier.setToolTipText("Modifier");
		boutonModifier.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				chargeProspectSelectionner();
			}
		});

		boutonImprimer.setIcon(new javax.swing.ImageIcon("src/main/java/com/photos/imprimer.gif"));
		boutonImprimer.setToolTipText("Imprimer");

		boutonRechercher.setIcon(new javax.swing.ImageIcon("src/main/java/com/photos/recherchermod.png"));
		boutonRechercher.setToolTipText("Rechercher");
		boutonRechercher.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				testFindProspect();
			}
		});

		jLabel2.setFont(new java.awt.Font("Verdana", 3, 14));

		panelhaut.add(jLabel1);

		jLabel2.setText("LISTE DES PROJETS");
		textRechercher.setPreferredSize(new Dimension(100, 27));
		textRechercher.setText("Numero");

		panelBouton1.add(boutonAjouter);
		panelBouton1.add(boutonSupprimer);
		panelBouton1.add(boutonModifier);

		panelBouton1.add(boutonImprimer);
		panelBouton1.add(boutonRechercher);
		panelBouton1.add(textRechercher);
		panelBouton1.add(labelDateDebut);
		panelBouton1.add(dateDebutProspect);
		panelBouton1.add(labelDateFin);
		panelBouton1.add(dateFinProspect);
		// panelBouton1.add(textLabel);
		// panelBouton1.add(jLabel2, BorderLayout.CENTER);
		panelBoutons.add(panelBouton1, BorderLayout.WEST);

		add(panelBoutons, java.awt.BorderLayout.CENTER);
		add(panelhaut, BorderLayout.NORTH);
		tableauListeProspect();// on fait appel à cette méthode pour le
								// chargement
								// du tableau
		panelbas.add(new JScrollPane(tableListeProspects), BorderLayout.CENTER);
		add(panelbas, BorderLayout.SOUTH);
		boutonSupprimer.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				final int[] selectionSupprimer = getTableListeProspects()
						.getSelectedRows();

				if (selectionSupprimer.length == 0) {
					JOptionPane.showMessageDialog(null,
							"veuillez selectionner un prospect d'abord!!!!");
				} else {
					if (JOptionPane.showConfirmDialog(getFrame(),
							"Voulez-vous vraiment supprimer ?", "Information",
							JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
						testDeleteProspects();

					}

				}

			}
		});

	}

	// méthode qui permet de lister les projets dans un JTable

	public JTextField getTextRechercher() {
		return textRechercher;
	}

	public void setTextRechercher(JTextField textRechercher) {
		this.textRechercher = textRechercher;
	}

	// methode qui permet de charger la liste des projets dans un JTable

	public void tableauListeProspect() {

		String[] entetes = { "Numero", "Type", "Commentaire",
				"Date de création" };

		List<Prospects> listProspects = prospectMetier.chargerAllProspects();
		int nombreLigne = listProspects.size();
		System.out.println("taille de la liste" + listProspects.size());
		Object data[][] = new Object[nombreLigne][5];

		// On parcoure la liste pour recuperer les propects et remplir le
		// tableau d'objet

		for (int i = 0; i < nombreLigne; i++) {

			prospect = listProspects.get(i);

			data[i][0] = prospect.getPrsNumero();
			data[i][1] = prospect.getPrsType();
			data[i][2] = prospect.getPrsComent();
			data[i][3] = prospect.getPrsDateCreate();
		}

		dtm = new DefaultTableModel(data, entetes);
		tableListeProspects = new JTable(dtm);

	}

	// methode pour la suppression d'un prospect

	public void testDeleteProspects() {

		// on recupére le nombre de ligne selectionné pour la suppression

		final int[] selection = getTableListeProspects().getSelectedRows();
		System.out.println("voici la taille selectionné " + selection.length);
		// test pour voir si la selection est vide
		if (selection.length == 0) {
			JOptionPane.showMessageDialog(null,
					"veuillez selectionner un prospect d'abord!!!!");

		} else {// si la selection n'est pas vide
			int taille = selection.length;

			for (int i = 0; i < taille; i++) {
				int index = selection[i];

				String prospectNumero = (String) getTableListeProspects()
						.getValueAt(index, 0);
				System.out
						.println(" voici le numero correspondant au prospect supprimer  "
								+ prospectNumero);

				Prospects prospectSupprimer = prospectMetier
						.findProspectByNumero(prospectNumero);
				prospectMetier.testSupprimerProspect(prospectSupprimer);

				dtm.removeRow(index);

			}
		}
	}

	// méthode pour la mise à jour d'un prospect
	public void chargeProspectSelectionner() {

		int ligneSelectionner = getTableListeProspects().getSelectedRow();
		if (ligneSelectionner == -1) {
			JOptionPane.showMessageDialog(null,
					"veuillez selectionner un prospect d'abord!!!!");

		} else {

			ProspectsMetier prospectMetier = new ProspectsMetier();
			Prospects prospectToModify = new Prospects();
			String numeroRecuperer = (String) getTableListeProspects()
					.getValueAt(ligneSelectionner, 0);
			String typeRecuperer = (String) getTableListeProspects()

			.getValueAt(ligneSelectionner, 1);
			String commentaireRecuperer = (String) getTableListeProspects()
					.getValueAt(ligneSelectionner, 2);
			Date dateCreation = (Date) getTableListeProspects().getValueAt(
					ligneSelectionner, 3);
			// On charge les données dans le panelModifierProspect

			PanelModifierProspect panModifierProspect = new PanelModifierProspect(
					buttonAnnuler);
			panModifierProspect.getTextNumero().setText(numeroRecuperer);
			panModifierProspect.getTexteCommentaire().setText(
					commentaireRecuperer);
			panModifierProspect.getDateCreateProspect().setDate(dateCreation);
			panModifierProspect.setNumeroCorrespondant(numeroRecuperer);

			boutonAjouter.setVisible(false);
			boutonSupprimer.setVisible(false);
			boutonImprimer.setVisible(false);
			boutonModifier.setVisible(false);
			boutonRechercher.setVisible(false);
			textRechercher.setVisible(false);
			labelDateDebut.setVisible(false);
			labelDateFin.setVisible(false);
			dateDebutProspect.setVisible(false);
			dateFinProspect.setVisible(false);

			panelbas.removeAll();
			panelbas.add(panModifierProspect, BorderLayout.SOUTH);
			validate();

		}
	}

	// méthode pour rechercher un prospect en fonction d'un numero

	public void testFindProspect() {

		ProspectsMetier prsMetier = new ProspectsMetier();
		Prospects prospectFined = new Prospects();
		System.out.println("voici le numero du prospect recherché   "
				+ textRechercher.getText());
		prospectFined = prsMetier
				.findProspectByNumero(textRechercher.getText());

		PanelModifierProspect panRechercherProspect = new PanelModifierProspect(
				buttonAnnuler);
		panRechercherProspect.getButtonValider().setVisible(false);
		buttonAnnuler.setText("RETOUR");
		panRechercherProspect.getTexteCommentaire().setText(
				prospectFined.getPrsComent());
		panRechercherProspect.getTextNumero().setText(
				prospectFined.getPrsNumero());
		panRechercherProspect.getDateCreateProspect().setDate(
				prospectFined.getPrsDateCreate());
		panelbas.removeAll();
		panelbas.add(panRechercherProspect, BorderLayout.SOUTH);
		validate();

	}

	public JTable getTableListeProspects() {
		return tableListeProspects;
	}

	public void setTableListeProspects(JTable tableListeProspects) {
		this.tableListeProspects = tableListeProspects;
	}

	public JPanel getFrame() {
		return this;

	}

	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		if(getTableListeProspects().getRowSelectionAllowed()==true){
			boutonSupprimer.setEnabled(true);
			boutonModifier.setEnabled(true);
		}
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}
